package cn.ctyun.ipush.web.user.service.impl;//package cn.ctyun.framework.user.service.impl;

import cn.ctyun.ipush.utils.ConfigUtils;
import cn.ctyun.ipush.web.user.bo.City;
import cn.ctyun.ipush.web.user.bo.UserModel;
import cn.ctyun.ipush.web.user.dao.UserDao;
import cn.ctyun.ipush.web.user.service.UserAdminService;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.spring.JdbcDaoImpl;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//
//import cn.ctyun.core.base.rest.RPCClient.RestPostClient;
//import cn.ctyun.core.base.rest.result.RESTStatusCode;
//import cn.ctyun.core.base.util.JsonUtils;
//import cn.ctyun.core.base.util.MD5;
//import cn.ctyun.framework.user.bo.SystemUserBO;
//import cn.ctyun.framework.user.service.UserAdminService;
//import cn.ctyun.utils.Tools;
//import org.apache.log4j.Logger;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.util.*;
//
///**
// * 编写成员：wangjia
// * 主要用途：
// * 实现说明：
// * 创建时间：2015-05-06
// */
@Service
public class UserAdminServiceImpl implements UserAdminService {

    private static final Logger logger = Logger
            .getLogger(UserAdminServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private JdbcDao jdbcDao;

    @Override
    public UserModel checkLogin(String systemUser,String userPassword ) {
        try {
            //String a = MD5.getMD5Code(userPassword);
           UserModel userModel = userDao.checkIsExist(systemUser, userPassword);
           if(userModel !=null){
                City city=new City();
                city.setCityId(userModel.getCity());
                city=jdbcDao.querySingleResult(city);
                if(city.getBelongType().contentEquals(ConfigUtils.getStringValue("startTaskBelongType"))){
                    return userModel;
                }else{
                    logger.info("用户不属于此主机");
                    return null;
                }
           }else{
               return null;
           }
        } catch (Exception e) {
            logger.error("IOException error " + e);
            return null;
        }
    }

    @Override
    public List<Map> getUserMenuByUserId(String userId) {

//        Map<String, String> headParam = new HashMap<String, String>();
//        List ls=new ArrayList();
//        try {
//            String requestURI = Tools.requestServerURI("getModuleByUserId");
//            headParam.put("userId", userId);
//            String result = postclient.callRestRPC(requestURI, headParam,
//                    null);
//            Map<String, Object> objectMap = JsonUtils.parseMap(result);
//            if (objectMap.get("returnObj") != null) {
//                ls =(List)objectMap.get("returnObj");
//            }
//        } catch (IOException e) {
//            logger.error("IOException error " + e);
//        }
//        return ls;
        return null;
    }

    @Override
    public List<UserModel> getUserAll(UserModel userModel){

        try {
            List<UserModel> userModelList = userDao.listAll(userModel);
            if(userModelList !=null){
                return userModelList;
            }else{
                return null;
            }
        } catch (Exception e) {
            logger.error("IOException error " + e);
            return null;
        }

    }
    @Override
    public boolean saveUser(UserModel userModel){
        return userDao.save(userModel);
    }
    @Override
    public boolean deleteUser(String userId){

        UserModel userModel = userDao.getUserById(userId);

        return  userDao.delete(userModel);

    }
    @Override
    public boolean updateUser(UserModel userModel){
        return userDao.update(userModel);
    }
    @Override
    public UserModel getUserById(String id){
        return  userDao.getUserById(id);
    }

}
